package com.ruoyi.project.g6.api;

import com.ruoyi.project.g6.entity.Auth;
import com.ruoyi.project.g6.entity.ChipPrefix;
import com.ruoyi.project.g6.entity.TboxModelAuthority;
import com.ruoyi.project.g6.entity.common.ResponseResult;
import com.ruoyi.project.g6.entity.vo.*;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import static com.ruoyi.project.g6.tools.HttpEntityBuild.buildParams;

@Component
public class Api {

    private static final RestTemplate REST_TEMPLATE = new RestTemplate();


    /**
     * 4.1 平台登录
     *
     * @param auth 包含用户名,密码的实体
     * @return token
     */
    public ResponseResult login(Auth auth) {
        return REST_TEMPLATE.postForEntity(ApiUrl.LOGIN, auth, ResponseResult.class).getBody();
    }

    /**
     * 4.2 发动机型号备案
     *
     * @param vehicleEngine 发动机引擎的数据json
     * @return
     */
    public ResponseResult insertVehicleEngine(VehicleEngineVO vehicleEngine) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_VEHICLE_ENGINE,
                buildParams(vehicleEngine),
                ResponseResult.class
        ).getBody();
    }

    /**
     * 4.3 车辆型号备案
     *
     * @param vehicleType 车辆型号数据json
     * @return
     */
    public ResponseResult insertVehicleType(VehicleTypeVO vehicleType) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_VEHICLE_TYPE,
                buildParams(vehicleType),
                ResponseResult.class
        ).getBody();
    }

    /**
     * 4.4 芯片型号备案
     *
     * @param chipModel 芯片型号实体
     * @return
     */
    public ResponseResult insertChipModel(ChipModelVO chipModel) {
        return REST_TEMPLATE.postForEntity(ApiUrl.INSERT_CHIP_MODEL,
                buildParams(chipModel),
                ResponseResult.class
        ).getBody();
    }

    /**
     * 4.5 芯片型号备案结果查询
     *
     * @param commonVo 申请芯片型号备案时获得的申请序号+令牌
     * @return
     */
    public ResponseResult insertChipModelResult(CommonVo commonVo) {

        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_CHIP_MODEL_RESULT,
                buildParams(commonVo),
                ResponseResult.class
        )
                .getBody();
    }


    /**
     * 4.6 已备案芯片型号查询
     * <p>
     * chipModel 芯片型号参数实体，为空时返回本企业所有已备案成功芯片型号
     * token     令牌
     *
     * @return
     */
    public ResponseResult getChipModel(GetChipModelVo getChipModelVo) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.GET_CHIP_MODEL,
                buildParams(getChipModelVo)
                , ResponseResult.class
        ).getBody();

    }

    /**
     * 4.7 芯片型号标识符申请
     *
     * @param chipPrefix 这里面包含令牌字段,如果为空的话会拿第二个参数进行设置
     * @param token      如果chipPrefix实体令牌字段不为空,忽略这个参数
     * @return
     */
    public ResponseResult getChipPrefix(ChipPrefix chipPrefix, String token) {
        if (chipPrefix != null && !StringUtils.hasText(chipPrefix.getToken())) {
            chipPrefix.setToken(token);
        }
        return REST_TEMPLATE.postForEntity(
                ApiUrl.GET_CHIP_PREFIX,
                buildParams(chipPrefix),
                ResponseResult.class
        ).getBody();
    }

    /**
     * 4.8 芯片型号标识符申请结果查询
     *
     * @param commonVo 申请芯片型号备案时获得的申请序号+令牌
     * @return
     */
    public ResponseResult getChipPrefixResult(CommonVo commonVo) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.GET_CHIP_PREFIX_RESULT,
                buildParams(commonVo),
                ResponseResult.class
        ).getBody();

    }

    /**
     * 4.9 车载终端型号备案
     */
    public ResponseResult insertTbox(TboxVO tbox) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_TBOX,
                buildParams(tbox),
                ResponseResult.class)
                .getBody();
    }

    /**
     * 4.10 车载终端型号备案结果查询
     *
     * @param commonVo 申请芯片型号备案时获得的申请序号+令牌
     * @return
     */
    public ResponseResult insertTboxModelResult(CommonVo commonVo) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_TBOX_MODEL_RESULT,
                buildParams(commonVo),
                ResponseResult.class).getBody();
    }

    /**
     * 4.11 已备案车载终端型号查询
     *
     * @return
     */
    public ResponseResult getTboxModel(GetTboxModelVo getTboxModelVo) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.GET_TBOX_MODEL,
                buildParams(getTboxModelVo),
                ResponseResult.class).getBody();

    }

    /**
     * 4.12 车载终端型号授权备案
     *
     * @param tboxModelAuthority 车组终端型号json实体
     * @return
     */
    public ResponseResult insertTboxModelAuthority(TboxModelAuthority tboxModelAuthority) {

        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_TBOX_MODEL_AUTHORITY,
                buildParams(tboxModelAuthority),
                ResponseResult.class
        ).getBody();

    }

    /**
     * 4.13 车载终端型号授权备案结果查询
     *
     * @param commonVo 申请芯片型号备案时获得的申请序号+令牌
     * @return
     */
    public ResponseResult insertTboxModelAuthorityResult(CommonVo commonVo) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_TBOX_MODEL_AUTHORITY_RESULT,
                buildParams(commonVo),
                ResponseResult.class
        ).getBody();

    }

    /**
     * 4.14 更换终端信息备案
     *
     * @return
     */
    public ResponseResult changeTbox(TboxChangeVO tboxChange) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.CHANGE_TBOX,
                buildParams(tboxChange),
                ResponseResult.class
        ).getBody();

    }

    /**
     * 4.15 更换终端信息备案结果查询
     *
     * @param commonVo 申请芯片型号备案时获得的申请序号+令牌
     * @return
     */
    public ResponseResult changeTboxResult(CommonVo commonVo) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.CHANGE_TBOX_RESULT,
                buildParams(commonVo),
                ResponseResult.class
        ).getBody();
    }

    /**
     * 4.16 车辆信息备案申请
     * 重型车远程排放服务与管理国家平台>5.4 车辆信息备案
     * 对于非必填的处理,非必填项应填写空字符串“”
     *
     * @param vehicle 车辆信息
     * @return
     */
    public ResponseResult insertVehicle(VehicleVO vehicle) {
        return REST_TEMPLATE.postForEntity(
                ApiUrl.INSERT_VEHICLE,
                buildParams(vehicle),
                ResponseResult.class
        ).getBody();

    }

    /**
     * 4.17 车辆状态查询
     *
     * @return
     */
    public ResponseResult vehicleStatus(GetVehicleStatusVO getVehicleStatusVO) {

        return REST_TEMPLATE.postForEntity(
                ApiUrl.VEHICLE_STATUS,
                buildParams(getVehicleStatusVO),
                ResponseResult.class
        ).getBody();
    }

    public static void main(String[] args) {
        Api api = new Api();
        Auth auth = new Auth();
        auth.setUsername("admin");
        auth.setPassword("123456");
        ResponseResult login = api.login(auth);
        System.out.println(login);
    }
}
